package cn.dazhou.networkdemo

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import cn.dazhou.networkdemo.databinding.ActivityMainBinding
import com.google.android.material.snackbar.Snackbar
import timber.log.Timber

class MainActivity : AppCompatActivity() {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initListener()

        registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
            if (it.all { entry -> Timber.i("${entry.key} = ${entry.value}"); entry.value }) {
                Timber.i("所有权限都获取到了^_^")
            }
        }.launch(arrayOf(Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_FINE_LOCATION))
    }

    @SuppressLint("SetTextI18n")
    private fun initListener() {
        binding.openDataEnabled.setOnClickListener { NetworkUtil.setDataEnabled(true) }
        binding.closeDataEnabled.setOnClickListener { NetworkUtil.setDataEnabled(false) }
        binding.isConnectNetwork.setOnClickListener { startActivity(Intent(this, IsConnectNetworkActivity::class.java)) }
        binding.preferredNetworkType.setOnClickListener { startActivity(Intent(this, PreferredNetworkTypeActivity::class.java)) }
        binding.is2G3G4G5G.setOnClickListener { binding.textView.text = NetworkUtil.get2G3G4G5G() }
        binding.getDefaultSlotIndex.setOnClickListener { startActivity(Intent(this, GetSimSlotIndexMainActivity::class.java)) }
        binding.getImeiIccid.setOnClickListener { startActivity(Intent(this, GetImeiActivity::class.java)) }
        binding.isSystemApp.setOnClickListener { binding.textView.text = "是否有系统权限：${Utils.isSharedUserIdSystemApp(this)}" }
        binding.isDataEnabled.setOnClickListener { showSnackbar(if (NetworkUtil.isDataEnabled()) "已开启" else "未开启") }
        binding.getDataState.setOnClickListener { showSnackbar(NetworkUtil.getDataState()) }
        binding.getPhoneNumber.setOnClickListener { binding.textView.text = NetworkUtil.getPhoneNumber() ?: "null" }
        binding.checkNetworkType.setOnClickListener { binding.textView.text = NetworkUtil.checkNetworkType(this) }
        binding.checkPhoneCount.setOnClickListener { binding.textView.text = NetworkUtil.checkPhoneType().toString() }
        binding.getSignal.setOnClickListener { binding.textView.text = NetworkUtil.getSignalStrength() }
        binding.getSimState.setOnClickListener { binding.textView.text = NetworkUtil.getSimState() }
        binding.requestCellInfo.setOnClickListener { NetworkUtil.requestCellInfos() }
    }


    private fun showSnackbar(text: CharSequence) {
        Snackbar.make(binding.root, text, Snackbar.LENGTH_SHORT).show()
    }
}